package com.quanyue.haircut.user.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.haircut.domain.constant.UserRole;
import com.quanyue.haircut.domain.entity.HaircutMerchant;
import com.quanyue.haircut.user.dto.MerchantDetail;
import com.quanyue.haircut.user.service.HaircutMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "商户")
@Scope(role = UserRole.MANAGER)
@RestController
@RequestMapping("/api")
public class HaircutMerchantController {
    @Autowired
    private HaircutMerchantService merchantService;

    @ApiOperation(value = "创建商户信息")
    @PostMapping(value = "/merchant/info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> createMerchant(@RequestBody HaircutMerchant merchant) {
        merchantService.addMerchant(merchant);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改商户信息")
    @PutMapping(value = "/merchant/info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> updateMerchant(@RequestBody HaircutMerchant merchant) {
        merchantService.updateMerchant(merchant);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除商户信息")
    @DeleteMapping(value = "/merchant/info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deleteMerchant(@RequestParam Integer id) {
        merchantService.deleteMerchant(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "查询商户信息")
    @GetMapping(value = "/merchant/info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <HaircutMerchant> createMerchant(@RequestParam Integer merchantNo) {
        return ServiceResult.success(merchantService.selectMerchantByNo(merchantNo));
    }

    @ApiOperation(value = "查询商户信息列表")
    @GetMapping(value = "/merchant/info/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <MerchantDetail>> createMerchant(@RequestParam Integer parentId, @ApiIgnore Principal principal) {
        if (parentId == 0) parentId = principal.getId();
        return ServiceResult.success(merchantService.selectMerchantList(parentId));
    }
}
